package com.zhuchang.self_rpc.test.democoding.nettydemo;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import jdk.nashorn.internal.ir.CallNode;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author RenPu
 * @Date 2022/2/10 15:12
 * @Version 1.0
 * @Description:  netty服务端业务逻辑的扩展handler
 **/
public class GroupChatServerHandle extends SimpleChannelInboundHandler<String> {

    //public static List<Channel> channels = new ArrayList<Channel>();

    //使用一个hashmap 管理
    //public static Map<String, Channel> channels = new HashMap<String,Channel>();

    //定义一个channle 组，管理所有的channel
    //GlobalEventExecutor.INSTANCE) 是全局的事件执行器，是一个单例

    public static ChannelGroup channelGroups=new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);


    //private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
    SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {

        Channel channel = channelHandlerContext.channel();
        channelGroups.forEach(ch->{
            if(ch!=channel){
                ch.writeAndFlush("【客户】"+ch.remoteAddress()+"发消息了："+s+"\n");
            }else {
                ch.writeAndFlush("【自己】 发送了消息"+ch.remoteAddress()+s+"\n");
            }
        });



    }



    //表示channel 处于活动状态, 提示 xx上线
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(ctx.channel().remoteAddress()+"上线了....(*^▽^*)....");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(ctx.channel().remoteAddress()+"离线了...o(╥﹏╥)o.....");
    }


    //handlerAdded 表示连接建立，一旦连接，第一个被执行
    //将当前channel 加入到  channelGroup
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        //将该客户加入聊天的信息推送给其它在线的客户端
        /*
        该方法会将 channelGroup 中所有的channel 遍历，并发送 消息，
        我们不需要自己遍历
         */
        Channel channel = ctx.channel();
        channelGroups.writeAndFlush("客户端："+channel.remoteAddress()+"加入聊天"+simpleDateFormat.format(new Date()));
        channelGroups.add(channel);
    }



    //断开连接, 将xx客户离开信息推送给当前在线的客户
    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        channelGroups.writeAndFlush("客户端："+channel.remoteAddress()+"断开连接"+simpleDateFormat.format(new Date()));
        System.out.println("defaultChannelGroup size :"+channelGroups.size());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }




}
